package com.air.controller;

import com.air.config.Result;
import com.air.entity.Flight;
import com.air.entity.Order;
import com.air.entity.Passenger;
import com.air.service.OrderService;
import com.air.service.PassengerService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Transactional
@Controller
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    PassengerService passengerService;


    @RequestMapping(value = "/getOrder",method = RequestMethod.GET)
    @ResponseBody
    public Result getOrderById(@RequestParam("orderId") int orderId, HttpSession session){
        Object id = session.getAttribute("id");
        if (id == null){
            return Result.fail("获取失败->用户未登录");
        }
        long userId = (long) id;
        Order order=orderService.getOrderById(orderId,userId);
        System.out.println("订单="+order);
        if (order != null){
            return Result.success("订单获取成功", order);
        }else{
            return Result.fail("获取失败->验证码不正确");
        }
    }

    @RequestMapping(value = "/getOrderList",method = RequestMethod.GET)
    @ResponseBody
    public Result getOrderById(HttpSession session){
        Object id = session.getAttribute("id");
        if (id == null){
            return Result.fail("获取失败->用户未登录");
        }
        long userId = (long) id;
        List<Order> order=orderService.getOrderList(userId);
        System.out.println("订单="+order);
        System.out.println(order.get(0).getFlight().getLandingAirportE());

        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();

        if (order != null){
            return Result.success("订单获取成功", order);
        }else{
            return Result.fail("获取失败->验证码不正确");
        }
    }

}
